package br.com.felipeab.dexter.bean;

import java.io.Serializable;
import java.util.Locale;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

@SessionScoped
@ManagedBean(name = "localeBean")
public class LocaleBean implements Serializable {

	private static final long serialVersionUID = -1767209511671229141L;

	private Locale locale;

	public Locale getLocale() {
		if (locale == null) {
			FacesContext context = FacesContext.getCurrentInstance();
			locale = context.getViewRoot().getLocale();
		}
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	public void localePortuguese(ActionEvent event) {
		FacesContext context = FacesContext.getCurrentInstance();
		context.getViewRoot().setLocale(new Locale("pt", "BR"));
		setLocale(context.getViewRoot().getLocale());
	}

	public void localeEnglish(ActionEvent event) {
		FacesContext context = FacesContext.getCurrentInstance();
		context.getViewRoot().setLocale(new Locale("en", "US"));
		setLocale(context.getViewRoot().getLocale());
	}
}
